﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BackspaceStringCompare
{
    public class Solution
    {
        public bool BackspaceCompare(string S, string T)
        {
            return this.ParseString(S) == this.ParseString(T);
        }

        private string ParseString(string s) 
        {
            var stack = new Stack<char>();
            foreach (var c in s)
            {
                if (c == '#')
                {
                    if (stack.Count > 0) 
                    {
                        stack.Pop();
                    }
                }
                else
                {
                    stack.Push(c);
                }
            }

            return string.Join("", stack);
        }
    }
}
